﻿using Net_SCADA.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace Net_SCADA
{
    public partial class Form_Alarm : fdBaseForm
    {
        int _pageIndex;
        int _totalPage=0;
        int _width = 10;
        int _height = 10;
        int _cols = 4;
        int _rows = 12;
        Dictionary<string,Panel[]> alarmPanel=new Dictionary<string, Panel[]>();
        Dictionary<string,Button> SelectButton=new Dictionary<string, Button>();
        string SelectedEquipID="";
        Panel LastPanel=null;
        Dictionary<string,bool> isEquipHasAlarm=new Dictionary<string, bool>();
        private Font[] equipButtonFont = new Font[] { new Font("Microsoft YaHei", 12f), new Font("Arial", 12f) };
        private Font[] alarmLabelFont = new Font[] { new Font("Microsoft YaHei", 9f), new Font("Arial", 9f) };

        public Form_Alarm()
        {
            InitializeComponent();
        }

        private void Form_Alarm_Load(object sender , EventArgs e)
        {
            netForms.SetPostionAndSize(this);
            this.Show();
            _pageIndex = 0;
            LoadInterface();
        }

        private void LoadInterface()
        {
            btn_next.Location = new Point(this.Width - netForms.sz.Width -5 , 5);
            btn_AlarmRecent.Location = new Point(5 , 5);
            btn_AlarmRecent.Size = btn_next.Size  = netForms.sz;
            btn_next.Visible = false;
            int pnlHeight=this.Height -lbl_AlarmTitle.Height -pnl_Select.Height -pnl_Bottom.Height;
            _width = (this.Width - 10 - (_cols - 1) * 4) / _cols;
            _height = (pnlHeight - (_rows - 1) * 4) / _rows;
            Size lblsz=new Size(_width,_height);

            //加载数据库获取有警报的设备的数量
            string[] alarmequip=DBOperate.GetAlarmEquipUsed();
            int _w=(this.Width -(alarmequip.Length +1)*2)/alarmequip.Length;
            if (alarmequip == null || alarmequip.Length == 0) return;
            for (int i = 0 ; i < alarmequip.Length ; i++)
            {
                isEquipHasAlarm.Add(alarmequip[i] , false);
                Button btn=new Button()
                {
                    Size=new Size(_w,pnl_Select.Height -4),
                    Text = NetLine.Equip[alarmequip[i]].EquipName[MultipleLanguage.systemLanguageIndex],
                    Font = equipButtonFont[MultipleLanguage.systemLanguageIndex],
                    FlatStyle=FlatStyle.Flat,
                    Location=new Point(2+i*(_w+2),2),
                    Tag= alarmequip[i],
                };
                SelectButton.Add(alarmequip[i] , btn);
                btn.Click += BtnAlarmEquip_Click;
                pnl_Select.Controls.Add(btn);
                _totalPage = (NetLine.Equip[alarmequip[i]].EquipAlarm.Count - 1) / (_cols * _rows) + 1;
                alarmPanel.Add(alarmequip[i] , new Panel[_totalPage]);
                int pnlindex=-1;
                for (int j = 0 ; j < NetLine.Equip[alarmequip[i]].EquipAlarm.Count; j++)
                {
                    if(j % (_rows * _cols) == 0)
                    {
                        alarmPanel[alarmequip[i]][++pnlindex] = new Panel()
                        {
                            Size = new Size(this.Width , pnlHeight) ,
                            Location = new Point(0 , pnl_Select.Top + pnl_Select.Height) ,
                            Visible = false
                        };
                        this.Controls.Add(alarmPanel[alarmequip[i]][pnlindex]);
                    }
                    Label lbl = new Label(){
                        Size=lblsz,
                        Location=new Point(5+(_width+4)*((j-pnlindex*(_cols*_rows)) % _cols ),(_height+4)*((j-pnlindex*(_cols*_rows))/_cols)),
                        Font = alarmLabelFont[MultipleLanguage.systemLanguageIndex],
                        AutoSize=false ,
                        BorderStyle=BorderStyle.FixedSingle,
                        TextAlign=ContentAlignment.MiddleCenter,
                        BackColor=Color.LightGreen,
                        Text = NetLine.Equip[alarmequip[i]].EquipAlarm[j].AlarmName[MultipleLanguage.systemLanguageIndex],
                        Tag=NetLine.Equip[alarmequip[i]].EquipAlarm[j]
                    };
                    lbl.Click += AlarmLabel_Click;
                    alarmPanel[alarmequip[i]][pnlindex].Controls.Add(lbl);
                }
            }
            SelectedEquipID = alarmequip[0];
            BtnAlarmEquip_Click(SelectButton[SelectedEquipID], null);
        }

        public void FlashAlarm()
        {
            foreach (var item in SelectButton.Keys)
            {
                bool isAlarm=false;
                for (int i = 0 ; i < alarmPanel[item].Length ; i++)
                {
                    foreach (Label lbl in alarmPanel[item][i].Controls)
                    {
                        Alarm al=lbl.Tag  as Alarm;
                        if (al.AlarmValue)
                        {
                            SelectButton[item].BackColor = Color.Red;
                            //SelectButton[item].ForeColor = Color.LightYellow;
                            isAlarm = true;
                            break;
                        }
                    }
                    if (isAlarm) break;
                }
                if (!isAlarm)
                {
                    SelectButton[item].BackColor = this.BackColor;
                    SelectButton[item].ForeColor = Color.Black;
                }
            }
            if(LastPanel!=null)
                ShowAlarmValue(LastPanel);
        }
        private void AlarmLabel_Click(object sender , EventArgs e)
        {
            //显示帮助
            Label lbl=sender as Label;
            Alarm al=lbl.Tag as Alarm;
            string alname = DBOperate.GetAlarmInfoFilename(al.AlarmTypeID);
            if (string.IsNullOrEmpty(alname)) return;
            string helpfile = $@"{Application.StartupPath}\Settings\AlarmInfo\{alname}{MultipleLanguage.systemLanguageIndex}.rtf";
            //StringBuilder helpfile=new StringBuilder().Append( Application.StartupPath).Append("\\Settings\\AlarmInfo\\").Append(DBOperate.GetAlarmInfoFilename(al.AlarmTypeID)).Append(".rtf");
            if (File.Exists(helpfile.ToString()))
            {
                Form_AlarmHelp fmAlarmHelp=new Form_AlarmHelp(al,helpfile);
                fmAlarmHelp.ShowDialog();
            }
        }

        private void BtnAlarmEquip_Click(object sender , EventArgs e)
        {
            Button btn= sender as Button;
            ShowAlarmPage(btn.Tag.ToString() , 0);
            lbl_AlarmTitle.Text = btn.Text;
            lblSubTitle.Left =(int) (fdPublicFunction.GetTextSize(lbl_AlarmTitle.Font,lbl_AlarmTitle.Text).Width*1.1f);
        }
        

        private void ShowAlarmPage(string Equipid , int pageindex)
        {
            if (alarmPanel[Equipid][pageindex] == LastPanel) return;
            if (LastPanel != null) LastPanel.Visible = false;
            _totalPage = alarmPanel[Equipid].Length; //(NetLine.Equip[Equipid].EquipAlarm.Count - 1) / (_cols * _rows) + 1;
            btn_next.Visible = _totalPage > 1;
            LastPanel = alarmPanel[Equipid][pageindex];
            LastPanel.Visible = true;
            //alarmPanel[Equipid][pageindex].BringToFront();
            //alarmPanel[SelectedEquipID].Visible = false;
            //alarmPanel[Equipid].Visible = true;
            //ShowAlarmValue(LastPanel);
            FlashAlarm();
            SelectedEquipID = Equipid;
            _pageIndex = pageindex;
        }

        private void ShowAlarmValue(Panel pnl)
        {
            //if (pnl == null) return;
            foreach (Label item in pnl.Controls)
            {
                Alarm al=item.Tag  as Alarm;
                if (al.AlarmValue)
                {
                    item.BackColor = NetLine.AlarmLevelColor[al.AlarmLevel][1];
                    item.ForeColor = NetLine.AlarmLevelColor[al.AlarmLevel][0];
                }
                else
                {
                    item.BackColor = Color.LightGreen;
                    item.ForeColor = Color.Black;
                }
            }
        }
        private void btn_AlarmRecent_Click(object sender , EventArgs e)
        {
            Form_AlarmRecent fmAlarRecent=new Form_AlarmRecent(); 
            fmAlarRecent.ShowDialog();  
        }
        private void btn_next_Click(object sender , EventArgs e)
        {
            if(++_pageIndex>(_totalPage-1))
                _pageIndex=0;
            ShowAlarmPage(SelectedEquipID , _pageIndex);
        }


   

        private void Form_Alarm_Activated(object sender , EventArgs e)
        {
            if (formLanguageIndex != MultipleLanguage.systemLanguageIndex)
            {
                formLanguageIndex = MultipleLanguage.systemLanguageIndex;
                foreach (var item in SelectButton.Keys)
                {
                    SelectButton[item].Text = NetLine.Equip[item].EquipName[formLanguageIndex];
                    SelectButton[item].Font = equipButtonFont[formLanguageIndex];
                }
                if(SelectedEquipID!="") lbl_AlarmTitle.Text = SelectButton[SelectedEquipID].Text;
                foreach (var item in alarmPanel.Values)
                {
                    foreach (var pnl in item)
                    {
                        foreach(var lbl in pnl.Controls)
                        {
                            if(lbl is Label)
                            {
                                Label label = lbl as Label;
                                if(label.Tag!=null)
                                {
                                    Alarm alarm=label.Tag as Alarm;
                                    label.Text = alarm.AlarmName[formLanguageIndex];
                                    label.Font = alarmLabelFont[formLanguageIndex];
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
